package com.eqdd.yiqidian.ui.mine.shenqing;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;

import com.eqdd.yiqidian.CreateNoticeActivityCustom;
import com.eqdd.yiqidian.R;
import com.eqdd.yiqidian.adapter.slimadapter.SlimInjector;
import com.eqdd.yiqidian.adapter.slimadapter.viewinjector.IViewInjector;
import com.eqdd.yiqidian.base.Config;
import com.eqdd.yiqidian.bean.rebuild.SearchPersonResultBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditCheckButtonBean;
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditEnterBean;
import com.eqdd.yiqidian.http.DialogCallBack;
import com.eqdd.yiqidian.http.HttpConfig;
import com.eqdd.yiqidian.http.HttpResult;
import com.eqdd.yiqidian.ui.base.SlimBaseMultiEditActivity;
import com.eqdd.yiqidian.utils.SPUtil;
import com.eqdd.yiqidian.utils.SingleChooseUtil;
import com.eqdd.yiqidian.utils.ToastUtil;
import com.eqdd.yiqidian.widget.ActionSheetDialog;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;

public class CreateContactBookActivity extends SlimBaseMultiEditActivity {
    private CreateNoticeActivityCustom dataBinding;
    private ActionSheetDialog chooseDialogRelate;
    private String msg;
    private SearchPersonResultBean searchPersonResultBean;

    @Override
    public void initBinding() {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_create_notice);
        initTopTitleBar(View.VISIBLE, "新建联络书");
        initTopRightText("发布", v -> {
            if (checkParam()) {
                OkGo.<HttpResult>post(HttpConfig.BASE_URL + HttpConfig.CREATE_CONTACT_BOOK)
                        .params("companyId", user.getCompanyId())
                        .params("userGuid", user.getGuid())
                        .params("departId", user.getDepartId())
                        .params("objectCompanyId", searchPersonResultBean.getCompanyId())
                        .params("objectDepartId", searchPersonResultBean.getDepartmentId())
                        .params("objecter", searchPersonResultBean.getGuid())
                        .params("liaisonBookName", datas.get(0).getContent())
                        .params("liaisonBookTheme", datas.get(2).getContent())
                        .params("liaisonBookContent", datas.get(3).getContent())
                        .params("timeLimit", datas.get(4).getContent())
                        .params("isReply", ((SlimEditCheckButtonBean) datas.get(5)).isCheck() ? "1" : "0")
                        .params("isLeader", user.getIsAdmin())
                        .execute(new DialogCallBack<HttpResult>(this) {
                            @Override
                            public void onSuccess(Response<HttpResult> response) {
                                HttpResult httpResult = response.body();
                                ToastUtil.showShort(httpResult.getMsg());
                                if (httpResult.getStatus() == 200) {
                                    isSave = false;
                                    clearData();
                                    setResult(Config.SUCCESS);
                                    finish();
                                }
                            }
                        });
            } else {
                ToastUtil.showShort(msg);
            }
        });
    }

    private boolean checkParam() {
        if (TextUtils.isEmpty(datas.get(0).getContent())) {
            msg = "请输入联络书名称";
            return false;
        }
        if (TextUtils.isEmpty(datas.get(1).getContent())) {
            msg = "请选择联系人";
            return false;
        }
        if (TextUtils.isEmpty(datas.get(2).getContent())) {
            msg = "请输入联络书主题";
            return false;
        }
        if (TextUtils.isEmpty(datas.get(3).getContent())) {
            msg = "请选择联络书内容";
            return false;
        }
        if (TextUtils.isEmpty(datas.get(4).getContent())) {
            msg = "请选择联络书周期";
            return false;
        }
        return true;
    }


    @Override
    protected RecyclerView initRecyclerView() {
        return dataBinding.recyclerView;
    }

    @Override
    public void itemClick(int p) {
        switch (slimAdapterEx.getDataItem(p).getPosition()) {
            case 1:
                Bundle bundle = new Bundle();
                startActivityForResult(SearchPersonActivity.class, bundle, Config.SEARCH_PERSON);
                break;
            case 4:
                if (chooseDialogRelate == null) {
                    SingleChooseUtil.getChooseDialog(CreateContactBookActivity.this, 28, (sheet) -> {
                        chooseDialogRelate = sheet;
                        sheet.show();
                    }, (position, strings) -> {
                        slimAdapterEx.getDataItem(p).setContent(strings.get(position - 1));
                        slimAdapterEx.notifyItemChanged(p);
                    });
                } else {
                    chooseDialogRelate.show();
                }
                break;
        }
    }

    @Override
    protected void setEditBeans() {

        datas.add(new SlimEditEnterBean("联络书名称", "", 0, "请输入联络书名称"));
        datas.add(new SlimEditEnterBean("联络人", "", 1, true, "请选择联络人"));
        datas.add(new SlimEditEnterBean("主题", "", 2, "请输入联络书主题"));
        datas.add(new SlimEditEnterBean("内容", "", 3, "请输入联络书内容"));
        datas.add(new SlimEditEnterBean("时限", "", 4, true, "请选择联络书时限"));
        datas.add(new SlimEditCheckButtonBean("是否回复", "", 5, false));
        slimAdapterEx.register(R.layout.list_item_24, new SlimInjector<SlimEditCheckButtonBean>() {
            @Override
            public void onInject(SlimEditCheckButtonBean data, IViewInjector injector) {
                injector.text(R.id.tv_left_center_content, data.getTitle())
                        .checked(R.id.sb_end, data.isCheck());
            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()) {

        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Config.SUCCESS && requestCode == Config.SEARCH_PERSON) {
            searchPersonResultBean = (SearchPersonResultBean) data.getSerializableExtra("bean");
            datas.get(1).setContent(searchPersonResultBean.getName());
            slimAdapterEx.notifyItemChanged(1);
        }
    }

}
